/*pub-2016-1-11*/
KSLITE.declare("tkapi-plugin", ["tkapi-util", "tkapi-config", "tkapi-replacebox"], function(a, b) {
	var c, d = KSLITE,
		e = (a("tkapi-config"), a("tkapi-util")),
		f = a("tkapi-replacebox"),
		g = {};
	g.tklink = function(a, b, e) {
		if (!d.iPO(b)) return null;
		var f, g = document.createElement("a");
		b.tc = this.getPluginName(), g.href = b.href, g.appendChild(document.createTextNode(a));
		for (f in b) b.hasOwnProperty(f) && "href" != f && g.setAttribute("data-" + f, b[f]);
		return e ? (c || (c = document.createElement("div")), c.innerHTML = "", c.appendChild(g), c.innerHTML) : g
	}, g.showReplaceBox = f.show;
	var h;
	g.getElementsFor = function(a) {
		var b = {};
		h = h || document.getElementsByTagName("*");
		for (var c, d = 0; d < h.length; d++) c = h[d].getAttribute("data-plugin"), c && (c.indexOf(",") && (c = c.split(",")[0]), b[c] || (b[c] = []), b[c].push(h[d]));
		return b[a] || []
	}, g.util = e;
	var i = {},
		j = function(a) {
			if (i[a]) return i[a];
			var b = function() {};
			b.prototype = g;
			var c = new b;
			return c.getPluginName = function() {
				return a
			}, i[a] = c, c
		},
		k = void 0,
		l = -1,
		m = 0,
		n = b.taskQueue = {
			queue: [],
			waiting: 0,
			onLoad: function() {
				var a = n,
					b = -1,
					c = a.queue[a.waiting],
					e = function() {
						0 !== b && (a.waiting++, clearTimeout(b), setTimeout(a.onLoad))
					};
				if (c) {
					if (a.queue[a.waiting] = m, c === l || !c.Def || !d.iF(c.Def)) return e();
					if (b = setTimeout(function() {
							e(), b = 0
						}, 2e3), c.Def.length) try {
						c.Def(e)
					} catch (f) {} else {
						try {
							c.Def()
						} catch (f) {}
						e()
					}
				}
			},
			attachPlugin: function(a, b) {
				var c = o(a),
					e = d.Env.mods[b],
					f = d.require(e.name);
				return e.fn({
					require: c
				}, f, d._ns(e.name)), e.status = 4, f
			},
			loadByName: function(a, b) {
				var c = this,
					e = "tkapi-plugin-" + b;
				d._lM({
					name: e
				}, function() {
					var d = c.attachPlugin(b, e);
					a(d)
				})
			},
			loadByDeclare: function(a, b) {
				var c = this,
					e = "tkapi-plugin-" + b.name;
				d.declare(e, [], b.declare);
				var f = c.attachPlugin(b.name, e);
				a(f), console.log("new declare", e)
			},
			loadByUrl: function(a, b) {
				var c = this,
					e = "tkapi-plugin-" + b.name;
				d.getScript(b.url, function() {
					var d = c.attachPlugin(b.name, e);
					a(d)
				}), console.log("new Url", b)
			},
			addTask: function(a, b) {
				var c = this,
					d = 0,
					e = c.queue.push(k) - 1,
					f = function(a) {
						clearTimeout(d), c.queue[e] = void 0 === a ? l : a, c.onLoad()
					};
				d = setTimeout(f, 1e4), b ? b(f, a.name) : a.declare ? c.loadByDeclare(f, a) : a.url ? c.loadByUrl(f, a) : a.name && c.loadByName(f, a.name)
			}
		},
		o = b.requireFor = function(a) {
			return function(b) {
				return "tkapi-plugin-api" == b ? j(a) : d.require(b)
			}
		};
	b.Def = function(a) {
		if (n.queue = [], n.waiting = 0, a && d.iA(a) && a.length)
			for (var b, c = 0; c < a.length; c++) b = a[c], n.addTask(b)
	}
});